package com.qk.management.listener;

import com.qk.management.service.impl.ReportOverviewServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.event.ApplicationStartedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

/**
 * 应用启动时自动清理旧缓存的监听器
 */
@Slf4j
@Component
public class CacheClearListener implements ApplicationListener<ApplicationStartedEvent> {

    @Autowired
    private ReportOverviewServiceImpl reportOverviewService;

    @Override
    public void onApplicationEvent(ApplicationStartedEvent event) {
        log.info("应用启动，开始清理旧缓存...");
        try {
            // 调用清理缓存方法
            reportOverviewService.clearOldCache();
            log.info("旧缓存清理完成");
        } catch (Exception e) {
            log.error("清理缓存时发生异常", e);
        }
    }
}